﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Atlandiz.Data;

namespace Atlandiz.EnvanterVeZimmetYonetimi
{
    public partial class ListeDepolar : DevExpress.XtraEditors.XtraForm
    {
        public bool ForSelection = false;

        public ListeDepolar()
        {
            InitializeComponent();
        }


        public void DepoYukle()
        {
            string SorguEk = "";

            if (Army.UserTipi != 1)
            {
                SorguEk += " AND Depolar.id IN (select id_depolar from KullaniciDepo where id_kullanici=" + Army.ActiveUserId + ") ";
            }


            _lwDepolar.Items.Clear();
            string Sorgu = "SELECT \n" +
                "Depolar.id [id],\n" +
                "Depolar.DepoAdi [Depo Adı],\n" +
                "Depolar.Aciklama [Açıklama],\n" +
                "Depolar.Adres [Adres],\n" +
                "Depolar.Telefon [Telefon],\n" +
                "Depolar.Fax [Fax],\n" +
                "Depolar.Email [Email],\n" +
                "Depolar.YetkiliAdSoyad\n" +
                "FROM Depolar\n" +
                "where 1=1 " + SorguEk + " AND Depolar.isDeleted=0";

            DataTable dt = Army.SQL().GetDataTable(Sorgu);

            foreach (DataRow item in dt.Rows)
            {
                ListViewItem ls = new ListViewItem(item["id"].ToString());
                ls.SubItems.Add(item["Depo Adı"].ToString());
                ls.SubItems.Add(item["Açıklama"].ToString());
                ls.SubItems.Add(item["Adres"].ToString());
                ls.SubItems.Add(item["Telefon"].ToString());
                ls.SubItems.Add(item["Fax"].ToString());
                ls.SubItems.Add(item["Email"].ToString());
                ls.SubItems.Add(item["YetkiliAdSoyad"].ToString());

                _lwDepolar.Items.Add(ls);
            }

        }



        private void DepoLst_Load(object sender, EventArgs e)
        {
            Army.EventConfirm = false;
            DepoYukle();
        }

        private void _btnKapat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void _btnYeni_Click(object sender, EventArgs e)
        {
            FormDepoTanitimKarti yenidepo = new FormDepoTanitimKarti();
            yenidepo.KayID=0;
            yenidepo.ShowDialog();
            DepoYukle();
        }

        private void _btnListeGuncelle_Click(object sender, EventArgs e)
        {
            DepoYukle();
        }

        private void silToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (_lwDepolar.SelectedItems.Count > 0) {
                PopupAreYouSure SilOnay = new PopupAreYouSure();
                Army.EventMessage = "Seçilen Depo Silinecek,\nEmin misiniz?";
                SilOnay.ShowDialog();

                if (Army.EventConfirm)
                {
                    Army.SQL().cmd("Update Depolar SET isDeleted=1 where id=" + _lwDepolar.SelectedItems[0].SubItems[0].Text.ToString());
                    MessageBox.Show("Seçilen Depo Silindi.","Kayıt Silindi.",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    DepoYukle();
                    //sil komutu
                }
            }

           

        }

        private void düzenleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_lwDepolar.SelectedItems.Count > 0) {
                FormDepoTanitimKarti depoduzenle = new FormDepoTanitimKarti();
                depoduzenle.KayID = int.Parse(_lwDepolar.SelectedItems[0].SubItems[0].Text.ToString());
                depoduzenle.ShowDialog();
                DepoYukle();

            }
        }

        private void _lwDepolar_DoubleClick(object sender, EventArgs e)
        {
            if (_lwDepolar.SelectedItems.Count > 0 & ForSelection)
            {
                Army.SecilenID = int.Parse(_lwDepolar.SelectedItems[0].SubItems[0].Text.ToString());
                Army.EventConfirm = true;
                this.Close();
            }
        }

        private void _btnAra_Click(object sender, EventArgs e)
        {
            _lwDepolar.Items.Clear();
            string Sorgu = "SELECT \n" +
                "Depolar.id [id],\n" +
                "Depolar.DepoAdi [Depo Adı],\n" +
                "Depolar.Aciklama [Açıklama],\n" +
                "Depolar.Adres [Adres],\n" +
                "Depolar.Telefon [Telefon],\n" +
                "Depolar.Fax [Fax],\n" +
                "Depolar.Email [Email],\n" +
                "Depolar.YetkiliAdSoyad\n" +
                "FROM Depolar\n" +
                "where Depolar.isDeleted=0 and \n"+
                   "Depolar.DepoAdi like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or \n" +
                   "Depolar.Aciklama like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or \n"+
                   "Depolar.Email like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or \n"+
                   "Depolar.YetkiliAdSoyad like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' ";

            DataTable dt = Army.SQL().GetDataTable(Sorgu);

            foreach (DataRow item in dt.Rows)
            {
                ListViewItem ls = new ListViewItem(item["id"].ToString());
                ls.SubItems.Add(item["Depo Adı"].ToString());
                ls.SubItems.Add(item["Açıklama"].ToString());
                ls.SubItems.Add(item["Adres"].ToString());
                ls.SubItems.Add(item["Telefon"].ToString());
                ls.SubItems.Add(item["Fax"].ToString());
                ls.SubItems.Add(item["Email"].ToString());
                ls.SubItems.Add(item["YetkiliAdSoyad"].ToString());

                _lwDepolar.Items.Add(ls);
            }
            _txtAraString.Focus();
        }
    }
}